package org.wheres2.utils;

import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import java.util.List;
import java.util.Map;
import org.wheres2.*;
import org.wheres2.core.contactlist.ContactManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class MySimpleAdapter extends SimpleExpandableListAdapter {
	
	private LayoutInflater mInflater;
	private List<Map<String, String>> tempChild;
	private List<List<Map<String,String>>> childItem;
	private ContactsDisplay context;
	public final static String EXTRA_USER = "org.wheres2.USERNAME";
	public final static String EXTRA_NAME = "org.wheres2.NAME";
	public final static String EXTRA_GROUP = "org.wheres2.GROUPPOS";
	
	static class ViewHolder{
		public TextView text1;
		public TextView text2;
		public ImageView imgReq;
		public ImageView imgPos;
	}

	public MySimpleAdapter(Activity context,
			List<Map<String, String>> groupData,int groupLayout,
			String[] groupFrom, int[] groupTo,
			List<List<Map<String, String>>> childData,
			int childLayout, String[] childFrom, int[] childTo) {
		
		
		super(context, groupData, groupLayout, groupFrom, groupTo, childData,
				childLayout, childFrom, childTo);
		childItem = childData;
		mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.context = (ContactsDisplay)context;
		 
	}
	
	@Override
	public View getChildView(int groupPosition,int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		tempChild = childItem.get(groupPosition);
		
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.my_simple_expandable_list_item_2, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.text1 = (TextView)convertView.findViewById(R.id.NameTextView);
			viewHolder.text2 = (TextView)convertView.findViewById(R.id.UsernameTextView);
			viewHolder.imgReq = (ImageView)convertView.findViewById(R.id.childImageRequest);
			viewHolder.imgPos = (ImageView)convertView.findViewById(R.id.childImagePosition);
			convertView.setTag(viewHolder);
		}
		
		final ViewHolder holder = (ViewHolder) convertView.getTag();
		
		final String name = tempChild.get(childPosition).get(ContactManager.NAME);
		final String user = tempChild.get(childPosition).get(ContactManager.USERNAME);
		final String request = tempChild.get(childPosition).get(ContactManager.POSITION_REQUEST);
		final String sent = tempChild.get(childPosition).get(ContactManager.POSITION_SENT);
		
		final int pos = groupPosition;
		
		holder.text1.setText(name);
		holder.text2.setText(user);
		if(request.equals(Boolean.toString(true))){
			holder.imgReq.setVisibility(View.VISIBLE);
			holder.imgReq.setImageResource(R.drawable.request_icon);
		}else{
			holder.imgReq.setVisibility(View.INVISIBLE);
		}
		
		if(sent.equals(Boolean.toString(true))){
			holder.imgPos.setVisibility(View.VISIBLE);
			holder.imgPos.setImageResource(R.drawable.ic_launcher);
		}else{
			holder.imgPos.setVisibility(View.INVISIBLE);			
		}
		
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				Intent intent= new Intent(context,ContactInteraction.class);
			    intent.putExtra(EXTRA_USER, user);
			    intent.putExtra(EXTRA_NAME, name);
			    intent.putExtra(EXTRA_GROUP, pos);
			    holder.imgPos.setVisibility(View.INVISIBLE);
			    holder.imgReq.setVisibility(View.INVISIBLE);
			    context.startActivity(intent);
					
			}
				 
		});
		
		convertView.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				
				removeContactDialog(user);
				
				return true;
			}
		});
		
	
		return convertView;
	} 
	
	
	/**
	 * 
	 * Visualizza la dialog che richiede la conferma di eliminazione del contatto
	 * 
	 * @param user il contatto da eliminare
	 */
	private void removeContactDialog(final String user){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(R.string.AskRemoveContactDialogMsg)
				.setPositiveButton(R.string.YesDialogButton, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				context.removeContactAction(user);
				
			}
		}).setNegativeButton(R.string.NoDialogButton, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {

				dialog.dismiss();
				
			}
		});
		builder.create().show();
		
	}
}
